package com.wushi.lenovo.asia5b.app_ui.other.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.mvp.base.BaseAppCompatActivity;
import com.mvp.common.GlobalValue;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_tab.NavigationActivity;
import com.wushi.lenovo.asia5b.app_utils.SystemUtil;

import butterknife.BindView;
import butterknife.OnClick;

import static timber.log.Timber.d;

public class SplashActivity extends BaseAppCompatActivity {

    @BindView(R.id.bt_splash_ok)
    Button bt_splash_ok;
    @BindView(R.id.rl_splash_vessel1)
    RelativeLayout rl_splash_vessel1;


    private boolean isFirstEnterApp = false;

    @Override
    protected int getLayoutId() {
        setStatusTrans();
        return R.layout.activity_splash;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println(String.format("--->>>>>>>>---SplashActivity开始启动:%s", System.currentTimeMillis()));
        isFirstEnterApp = GlobalValue.isFirstLaunch();
        d("--->>>>>>>>---isFirstEnterApp:" + isFirstEnterApp);

        //-----------增加这段代码是为了解决release安装包安装后打开app后再按home键再次进入SplashActivity的BUG----------start
        if (!this.isTaskRoot()) {
            Intent intent = getIntent();
            if (intent != null) {
                String action = intent.getAction();
                if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(action)) {
                    finish();
                    return;
                }
            }
        }

        initView();

        initData();

        initEvent();

        String deviceBrand = SystemUtil.getDeviceBrand();
        String systemModel = SystemUtil.getSystemModel();
        String systemVersion = SystemUtil.getSystemVersion();
        d("--->>>>>>>>---" + String.format("手机厂商:%s,手机厂商:%s,安卓系统版本号:%s", deviceBrand, systemModel, systemVersion));
    }

    @Override
    protected void initView() {
        rl_splash_vessel1.setVisibility(isFirstEnterApp ? View.VISIBLE : View.GONE);
    }

    @Override
    protected void initData() {
        if (!isFirstEnterApp) {
            startMainActivity();
        }
    }

    @Override
    protected void initEvent() {

    }

    @Override
    @OnClick({
            R.id.bt_splash_ok,
            R.id.ll_splash
    })
    public void onClick(View v) {
        if (v.getId() == R.id.bt_splash_ok) {
            startWelcomeActivity();
        } else if (v.getId() == R.id.ll_splash) {
            if (GlobalValue.isDebug) {

            }
        }
    }

    public void startWelcomeActivity() {
        GlobalValue.saveFirstLaunch(false);
        startActivity(new Intent(this, WelcomeActivity.class));
//        startActivity(new Intent(this, LoginActivity.class));
        finish();
    }

    public void startMainActivity() {
        d("--->>>>>>>>---startMainActivity");
        Intent intent = new Intent(this, MainActivity.class);
//        Intent intent = new Intent(this, NavigationActivity.class);
        /**
         * todo 从第三方应用(facebook)跳转而来的数据
         */
        if (null != getIntent().getData()) {
            intent.setData(getIntent().getData());
        }
        startActivity(intent);
        finish();
    }

}
